<?php
/**
 * Created by PhpStorm.
 * User: xiong <1099446868@qq.com>
 * DateTime: 2018/11/7
 * TIME: 18:38
 */

namespace app\admin\model;


use think\Db;

class Article extends BaseModel {

    public static function RecommendForyou() {
        $arr = self::where('id', '<', 20)->select();
        return $arr;
    }

    public static function getArticleById($id) {
        $arr = self::get($id);
        return $arr;
    }

    public static function getArticleByClass($class, $page) {

        $arr = self::where('lm', $class)->page($page, 15)->select();
        return $arr;
    }

    public static function dao($class, $page) {
        $all = self::table($class)->page($page, 1000)->select();

        foreach ($all as $k => $v) {

            self::create([
                'lm'         => $v['lm'],
                'flm'        => $v['flm'],
                'title'      => $v['title'],
                'content'    => $v['content'],
                'author'     => $v['author'],
                'hits'       => $v['hits'],
                'createtime' => $v['createtime'],
                'oldurl'     => $v['oldurl'],
                'status'     => $v['status'],
                'isown'      => $v['isonw']
            ]);

        }

        return true;
    }

    public static function dao2($class, $page) {
        $all = self::page($page, 2000)->select();

        foreach ($all as $k => $v) {
            Db::name($class)->insert([
                'lm'         => $v['lm'],
                'flm'        => $v['flm'],
                'title'      => $v['title'],
                'author'     => $v['author'],
                'hits'       => $v['hits'],
                'createtime' => $v['createtime'],
                'oldurl'     => $v['oldurl'],
                'status'     => $v['status'],
                'isown'      => $v['isonw']
            ]);
            $id = Db::name($class)->getLastInsID();
            Db::name($class . 'Text')->insert([
                'id'      => $id,
                'cid'     => $id,
                'content' => $v['content']
            ]);
        }

        return $id;
    }

    //一次性导入所有记录
    public static function dao3() {
        $class = 'it';
        $count = self::count();
        $tiao = 500;   //每次导入记录数
        $ci = ceil($count / $tiao);   //一个需要多少次
        for ($i = 0; $i <= $ci; $i++) {
            $all = self::page($i, $tiao)->select();
            foreach ($all as $k => $v) {
                Db::name($class)->insert([
                    'lm'         => $v['lm'],
                    'flm'        => $v['flm'],
                    'title'      => $v['title'],
                    'author'     => $v['author'],
                    'hits'       => $v['hits'],
                    'createtime' => $v['createtime'],
                    'oldurl'     => $v['oldurl'],
                    'status'     => $v['status'],
                    'isown'      => $v['isonw']
                ]);
                $id = Db::name($class)->getLastInsID();
                Db::name($class . 'Text')->insert([
                    'id'      => $id,
                    'cid'     => $id,
                    'content' => $v['content']
                ]);
            }
            echo '记录循环导入中，当前id是' . $id . ',还需要' . $ci - $i . '次<br>';
        }
        echo '全部记录导入完成<br>';
    }

    //
    public static function dao4($page) {
        $class = 'it';
        $all = self::page($page, 2000)->select();
        foreach ($all as $k => $v) {
            Db::name($class)->insert([
                'lm'         => $v['lm'],
                'flm'        => $v['flm'],
                'title'      => $v['title'],
                'author'     => $v['author'],
                'hits'       => $v['hits'],
                'createtime' => $v['createtime'],
                'oldurl'     => $v['oldurl'],
                'status'     => $v['status'],
                'isown'      => $v['isonw']
            ]);
            $id = Db::name($class)->getLastInsID();
            Db::name($class . 'Text')->insert([
                'id'      => $id,
                'cid'     => $id,
                'content' => $v['content']
            ]);
        }
        return $id;
    }

    public static function dao5($class, $page) {
        if(empty($class) || empty($page))
            exit('table or page error!');
        $all = self::name($class)->page($page, 200)->select();
        //dump($all);exit;
        foreach ($all as $k => $v) {
            Db::name('article')->insert([
                'lm'         => $v['lm'],
                'flm'        => $v['flm'],
                'title'      => $v['title'],
                'author'     => $v['author'],
                'hits'       => $v['hits'],
                'createtime' => $v['createtime'],
                'oldurl'     => $v['oldurl'],
                'status'     => $v['status'],
                'isown'      => $v['isown']
            ]);
            $id = Db::name('article')->getLastInsID();
            Db::name('article_text')->insert([
                'id'      => $id,
                'cid'     => $id,
                'content' => $v['content']
            ]);
        }
        $id = !isset($id) || empty($id) ? 'unknow error' : $id;
        return $id;
    }

    public static function newTableCount($table){
        return Db::name($table)->count('id');
    }

    public static function getArticleCount(){
        return self::count('id');
    }

}